<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Shop\Mark;

class MarkController extends Controller
{
    public function __construct()
    {
        view()->share([
            '_shop' => 'active open',
            '_mark' => 'active',
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = function ($query) use ($request) {
            if ($request->has('keyword') and $request->keyword != '') {
                $search = "%" . $request->keyword . "%";
                $query->where('name', 'like', $search);
            }
        };
        $marks = Mark::with('goods')->where($where)->orderBy('sort_order', 'desc')
            ->paginate(env('pageSize'));

        return view('admin.shop.mark.index', compact('marks'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.shop.mark.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|unique:marks|max:255',
            'url' => 'required|url',
            'sort_order' => 'required|integer|max:99',
            'description' => 'required'
        ]);

        Mark::create($request->all());
        return redirect(route('shop.mark.index'))->with('success', '新增品牌成功~');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $mark = Mark::find($id);
        return view('admin.shop.mark.edit', ['mark' => $mark]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required|max:255',
            'url' => 'required|url',
            'sort_order' => 'required|integer|max:99',
            'description' => 'required'
        ]);

        $mark = Mark::find($id);
        $mark->update($request->all());
        return redirect(route('shop.mark.index'))->with('success', '编辑品牌成功~');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if (!Mark::check_goods($id)) {
            return back()->with('error', '该品牌下有商品，不能删除');
        }
        Mark::destroy($id);
        return back()->with('success', '删除品牌成功~');
    }


    /**
     * ajax排序
     * @param Request $request
     */
    public function sort_order(Request $request)
    {
        $mark = Mark::find($request->id);
        $mark->sort_order = $request->sort_order;
        $mark->save();

    }


    /**
     * ajax删除多条
     * @param Request $request
     */
    public function delete_more(Request $request)
    {
        $checked_id = $request->checked_id;
        Mark::destroy($checked_id);
    }


    /**
     * ajax改变属性
     * @param Request $request
     */
    public function change_attr(Request $request)
    {
        $mark = Mark::find($request->id);
        $attr = $request->attr;
        $mark->$attr = !$mark->$attr;
        $mark->save();
    }
}
